﻿using AutoMapper;
using MediatR;
using MyMES.Domain.zyy;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Writer.API.Application.Commends.Productionwork;

namespace MyMES.Writer.API.Application.CommendHandlers.Productionwork
{
    public class updataProductionworkCommendHandlers : IRequestHandler<UpdataProductionworkCommends, APIResult<WorkOrder>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<WorkOrder> workorderRepository;

        public updataProductionworkCommendHandlers(IMapper mapper, IBaseRepository<WorkOrder> workorderRepository)
        {
            this.mapper = mapper;
            this.workorderRepository = workorderRepository;
        }

        /// <summary>
        /// 更新生产工单
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<WorkOrder>> Handle(UpdataProductionworkCommends request, CancellationToken cancellationToken)
        {
            APIResult<WorkOrder> result = new APIResult<WorkOrder>();
            result.Code = APIEnum.修改成功;
            result.Msg = "修改成功";
            var model = mapper.Map<WorkOrder>(request);
            string sql = "update WorkOrder set WorkOrderCoding=@WorkOrderCoding,WorkOrderName=@WorkOrderName,WorkOrderType=@WorkOrderType,WorkorderSource=@WorkorderSource,OrderNumber=@OrderNumber,ProductNumber=@ProductNumber,ProductName=@ProductName,Specifications=@Specifications,Unit=@Unit,WorkOrderNum=@WorkOrderNum,AdjustQuantity=@AdjustQuantity,QuantityProduced=@QuantityProduced,LotNumber=@LotNumber,CustomerCode=@CustomerCode,CustomerNamwe=@CustomerNamwe,RequestDate=@RequestDate,State=@State,SupplierCode=@SupplierCode,SupplierName=@SupplierName,Note=@Note,IsDlete=@IsDlete where WorkOrderId=@WorkOrderId";
            await workorderRepository.Update(model, sql);
            return result;
        }
    }
}
